<template>
  <div class="user-page">
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="编号" width="300"></el-table-column>
      <el-table-column prop="real_name" label="昵称" width="180"></el-table-column>
      <el-table-column prop="phoneNum" label="手机号"></el-table-column>
      <el-table-column prop="student_crad_pic" label="头像">
        <template v-slot="scope">
          <el-avatar
            size="medium"
            :src="
              scope.row.student_crad_pic
             ? scope.row.student_crad_pic
              : 'https://img0.baidu.com/it/u=739011191,1281948250&fm=253&fmt=auto&app=138&f=PNG?w=475&h=475'
            "
          ></el-avatar>
        </template>
      </el-table-column>

      <el-table-column width="180" label="操作">
        <template slot-scope="scope">
          <el-button @click="edit(scope.row.id)" size="small" type="primary">编辑</el-button>
          <el-button @click="del(scope.row.id)" size="small" type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import { userAll, delUser } from "@/api/users.js";
export default {
  data() {
    return {
      userList: [],
      userId: "",
      // 当前页码，初始化为1
      currentPage: 1,
      // 每页显示的条数，默认设置为10，可选项还有20
      pageSize: 10,
      // 总数据条数，初始为0，后续根据接口返回数据更新
      total: 0,
      // 用于表格展示的当前页数据
      tableData: []
    };
  },
  methods: {
    // 获取全部用户信息
    async getUserList() {
      const res = await userAll();
      // console.log(res)
      this.userList = res.users;
      this.total = this.userList.length;
      this.getTableData();
    },
    // 根据当前页码和每页显示条数获取当前页数据
    getTableData() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      this.tableData = this.userList.slice(start, end);
    },
    // 每页条数改变时的处理函数
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getTableData();
    },
    // 当前页码改变时的处理函数
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getTableData();
    },
    edit(id) {
      console.log(id);
    },
    async del(id) {
      try {
        const res = await delUser(id);
        if (res.status == 0) {
          this.getUserList();
        }
      } catch (error) {
        console.log(error);
      }
    },
  },
  created() {
    this.getUserList();
  },
};
</script>

<style scoped>
/* 整体页面容器样式 */
.user-page {
  padding: 20px;
  background-color: #f8f9fa;
}

/* 搜索栏样式（若后续有需求可添加搜索栏，此处先预留样式类） */
.search-bar {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}
.search-bar el-input {
  border-radius: 4px;
  border: 1px solid #ccc;
  padding: 8px 10px;
  flex: 1;
}
.search-bar el-button {
  margin-left: 10px;
  border-radius: 4px;
  padding: 6px 12px;
  transition: all 0.3s ease;
}
.search-bar el-button:hover {
  opacity: 0.8;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}

/* 表格样式 */
.el-table {
  background-color: white;
  border-radius: 5px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  margin-bottom: 20px;
}
.el-table th {
  background-color: #f0f0f0;
  color: #333;
  font-weight: normal;
  text-align: center;
  padding: 12px 0;
}
.el-table td {
  text-align: center;
  padding: 10px 0;
}
.el-table.el-avatar {
  margin: 0 auto;
  border-radius: 50%;
}
.el-table.el-button {
  margin: 0 5px;
  padding: 6px 12px;
  border-radius: 3px;
  transition: all 0.3s ease;
}
.el-table.el-button:hover {
  opacity: 0.8;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}

/* 表头文字样式 */
.el-table th >.cell {
  font-size: 14px;
}

/* 表格内容文字样式 */
.el-table td >.cell {
  font-size: 13px;
}

/* 分页组件样式 */
.el-pagination {
  text-align: center;
  margin-top: 10px;
}
.el-pagination.is-background.el-pager li {
  background-color: #f0f0f0;
  color: #333;
  border-radius: 3px;
  margin: 0 3px;
  transition: all 0.3s ease;
}
.el-pagination.is-background.el-pager li:hover {
  background-color: #e0e0e0;
  cursor: pointer;
}
.el-pagination.is-background.el-pager li.active {
  background-color: #13ce66;
  color: white;
}
.el-pagination button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}
</style>